package com.watchthelan.web.login;

import java.net.BindException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import com.watchthelan.domain.Administrator;
import com.watchthelan.service.administrator.AdministratorManager;

public class LoginFormController extends SimpleFormController {
	protected final Log logger = LogFactory.getLog(getClass());
	
	private HttpSession session;

	private AdministratorManager administratorManager;

	public ModelAndView onSubmit(Object command)
	throws ServletException {
		Administrator administrator = (Administrator) command;
		session.setAttribute("administrator", administrator);
		session.setAttribute("adminName", administrator.getName());
		logger.info("put administrator on session");
		return new ModelAndView(new RedirectView(getSuccessView()));
	}

	protected Object formBackingObject(HttpServletRequest request) throws ServletException {
		Administrator administrator = new Administrator();
		session = request.getSession();
		return administrator;
	}	

	public AdministratorManager getAdministratorManager() {
		return administratorManager;
	}

	public void setAdministratorManager(AdministratorManager administratorManager) {
		this.administratorManager = administratorManager;
	}
}
